#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
using namespace std;

class Solution {

    int vis[50][50] = { 0 };
    int m;
    int n;
    int ret = 0;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int size = 0;
    void dfs(vector<vector<int>>& grid, int i, int j)
    {
        ++size;
        for (int k = 0; k < 4; ++k)
        {
            int x = i + dx[k], y = j + dy[k];
            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && !vis[x][y])
            {
                vis[x][y] = true;
                dfs(grid, x, y);
            }
        }
    }
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        m = grid.size();
        n = grid[0].size();
        int ret = 0;
        for (int i = 0; i < m; ++i)
            for (int j = 0; j < n; ++j)
                if (grid[i][j] == 1 && !vis[i][j])
                {
                    size = 0;
                    vis[i][j] = true;
                    dfs(grid, i, j);
                    ret = max(ret, size);
                }
        return ret;
    }
};